/**
 * 
 */
package unseen.games.ui.manager;

import unseen.games.config.Config;
import unseen.games.taptap.MainActivity;
import unseen.games.taptap.R;
import unseen.games.ui.GenericScreenManager;
import unseen.games.view.GenericView;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.TextView;

/**
 * @author PERRAUDIN Steven <sperraudin@neotilus.com>
 *
 * @date 15 juin 2010
 */
public class IngameManager extends GenericScreenManager implements OnTouchListener{

	private View mIngameView;
	private Context mContext;
	
	private TextView mScore;
	private TextView mTime;
	
	private GenericView mGlSurfaceView;
	
	private int tapScore;
	private int time;
	
	private static final int WAITING 	= 0;
	private static final int INGAME 	= 1;
	private static final int FINISHED 	= 2;
	
	
	private int ingame;
	
	private Handler mHandler = new Handler();
	
	private Display display;
	
	public IngameManager(Context context, View view) {
		super(context);
		
		mContext = context;
		
		initView(view);
	}

	public void initGame() {
		tapScore 	= 0;
		ingame 		= WAITING;
		
		mScore.setText("Tap Tap : 0");
		mTime.setText("Time : 0");
	}

	private void initView(View view) {
		mIngameView = view;
		
		mScore 			= (TextView) mIngameView.findViewById(R.id.score);
		mTime 			= (TextView) mIngameView.findViewById(R.id.time);
		mGlSurfaceView	= (GenericView) mIngameView.findViewById(R.id.glsurface_view);

		display 		= ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		
		mGlSurfaceView.getRenderer().setDisplay(display);
		
		mIngameView.setOnTouchListener(this);
	}

	private void startGame() {		
		time = 0;
		mHandler.postDelayed(mRefreshTimer, 1000);
	}
	
	private void endGame(){
		AlertDialog.Builder builder = new AlertDialog.Builder(((MainActivity)mContext));
		builder.setMessage("Do you want to save your score ?")
		       .setCancelable(false)
		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   ((MainActivity)mContext).showHighScores(tapScore, true);
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	   ((MainActivity)mContext).showMainMenu();
		           }
		       });
		AlertDialog alert = builder.create();
		alert.show();
	}

	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			if(ingame == WAITING){
				startGame();
				ingame = INGAME;
			}
			else if(ingame == INGAME){
				tapScore++;
				mScore.setText("Tap Tap : " + tapScore);
				mGlSurfaceView.getRenderer().addWave(event);
			}
		}
		return true;
	}
	
	private Runnable mRefreshTimer = new Runnable() {
		public void run() {
			time++;
				
			if(time > Config.TIME_LIMIT){
				ingame = FINISHED;
				mHandler.removeCallbacks(mRefreshTimer);
				endGame();
				return;
			}
				
			mTime.setText("Time : " + time);
			mHandler.postDelayed(mRefreshTimer, 1000);
		}
	};

}
